package cn.zime.iot.view;

import cn.zime.iot.handler.LoginHandler;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class LoginView extends JFrame {
    JLabel nameLabel = new JLabel("欢迎来到ZIME·IoT2202学生成绩管理系统", JLabel.CENTER);
    JLabel authorLabel = new JLabel("开发者：童佳豪 钱玉科 缪茂钻 宋品璋 戚俊豪 潘易成", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();//弹簧布局
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("用户名:");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密  码:");
    JPasswordField pwdField = new JPasswordField();
    JButton loginBtn = new JButton("登录");
    JButton cancelBtn = new JButton("取消");
    JButton registerAccountBtn = new JButton("注册账号");
    JButton findPasswordBtn = new JButton("找回密码");

    LoginHandler loginHandler;

    public LoginView() {
        setTitle("ZIME·IoT2202学生成绩管理系统");

        loginHandler = new LoginHandler(this);

        Container contentPane = getContentPane();//内容面板

        nameLabel.setFont(new Font("华文楷体", Font.PLAIN, 27));
        nameLabel.setPreferredSize(new Dimension(0, 80));
        authorLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        authorLabel.setPreferredSize(new Dimension(0, 80));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        Font otherFont = new Font("楷体", Font.PLAIN, 18);
        userNameLabel.setFont(centerFont);
        userNameLabel.setIcon(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/账号.png"))));
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdLabel.setIcon(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/密码.png"))));
        pwdField.setPreferredSize(new Dimension(200, 30));
        loginBtn.setFont(centerFont);
        loginBtn.setIcon(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/登录.png"))));
        cancelBtn.setFont(centerFont);
        cancelBtn.setIcon(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/取消.png"))));
        registerAccountBtn.setFont(otherFont);
        findPasswordBtn.setFont(otherFont);
        //把组件加入面板
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        loginBtn.addActionListener(loginHandler);
        loginBtn.addKeyListener(loginHandler);//增加按键事件
        centerPanel.add(loginBtn);
        cancelBtn.addActionListener(loginHandler);
        centerPanel.add(cancelBtn);

        registerAccountBtn.addActionListener(loginHandler);
        centerPanel.add(registerAccountBtn);
        findPasswordBtn.addActionListener(loginHandler);
        centerPanel.add(findPasswordBtn);
        layoutCenter();//弹簧布局



        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(authorLabel, BorderLayout.SOUTH);


        getRootPane().setDefaultButton(loginBtn);//设置默认按钮

        //自定义图标
        setIconImage(new ImageIcon(Objects.requireNonNull(RegisterAccountView.class.getResource("/image/1.png"))).getImage());
        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);


    }

    /**
     * 弹簧布局
     */
    private void layoutCenter() {

        //布局userNameLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)),
                Spring.constant(20));
        int offsetX = childWidth.getValue() / 2 + 65;
        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX,
                SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
        //userTxt
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
        //pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 30, SpringLayout.SOUTH, userNameLabel);
        //pwdField
        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);
        //loginBtn
        springLayout.putConstraint(SpringLayout.WEST, loginBtn, 80, SpringLayout.WEST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 50, SpringLayout.SOUTH, pwdLabel);
        //cancelBtn
        springLayout.putConstraint(SpringLayout.WEST, cancelBtn, 100, SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, cancelBtn, 0, SpringLayout.NORTH, loginBtn);
        //registerAccountBtn
        springLayout.putConstraint(SpringLayout.WEST, registerAccountBtn, 20, SpringLayout.EAST, userTxt);
        springLayout.putConstraint(SpringLayout.NORTH, registerAccountBtn, 0, SpringLayout.NORTH, userTxt);
        //findPasswordBtn
        springLayout.putConstraint(SpringLayout.WEST, findPasswordBtn, 20, SpringLayout.EAST, pwdField);
        springLayout.putConstraint(SpringLayout.NORTH, findPasswordBtn, 0, SpringLayout.NORTH, pwdField);


    }

    public static void main(String[] args) {
        new LoginView();
    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

}
